// gulp 文件入口
const { series, parallel, src, dest } = require("gulp");
const fs = require("fs");
const { Transform } = require("stream");
const cleanCss = require("gulp-clean-css");
const rename = require("gulp-rename");
// 取消了同步代码格式 每个函数必须是异步
// 推荐导出成员方式
exports.foo = (done) => {
  console.log("foo task working~~");
  // 标识以下任务完成
  done();
};
// exports.default = (done) => {
//   console.log("default task working~~");
//   // 标识以下任务完成
//   done();
// };
// 4.0以前 不推荐
// gulp模块的方法实现
const gulp = require("gulp");
gulp.task("bar", (done) => {
  console.log("bar working!");
  done();
});

// 并行执行 =============================================
const task1 = (done) => {
  setTimeout(() => {
    console.log("task1 working~~");
    done();
  }, 1000);
};
const task2 = (done) => {
  setTimeout(() => {
    console.log("task2 working~~");
    done();
  }, 1000);
};
const task3 = (done) => {
  setTimeout(() => {
    console.log("task3 working~~");
    done();
  }, 1000);
};
// 串行
exports.series = series(task1, task2, task3);
// 并行
exports.parallel = parallel(task1, task2, task3);
// 编译css和js互不干扰 => 并行执行
// 部署需要先编译 => 串行

// 异步任务 =============================================
// 如何通知gulp完成与否
// 错误优先
exports.callback = (done) => {
  console.log("callback task working~~");
  done();
};
exports.callbackError = (done) => {
  console.log("callbackError task working~~");
  done(new Error("callbackError task failed~~"));
};
// promise方案可以避免回调嵌套过深
exports.promise = () => {
  console.log("promise task working~~");
  // resolve()里面的值会被忽略掉
  return Promise.resolve();
};

exports.promiseError = () => {
  console.log("promiseError task working~~");
  // resolve()里面的值会被忽略掉
  return Promise.reject(new Error("promiseError task failed~~"));
};

const timeout = (time) => {
  return new Promise((resolve) => {
    setTimeout(resolve, time);
  });
};
// setTimeout(() => {

// }, timeout);
// async await
exports.async = async () => {
  await timeout(1000);
  console.log("async works");
};
// stream
// 读取完成触发end事件 gulp就知道任务完成了
exports.stream = (done) => {
  const readStream = fs.createReadStream("package.json");
  const writeStream = fs.createWriteStream("temp.txt");
  readStream.pipe(writeStream);
  // 默认on end就完事儿
  readStream.on("end", () => {
    done();
  });
  return readStream;
};
// gulp-build process ==============================
// 此时不需要done 默认返回了一个on
// exports.default = () => {
//   // 文件读取流
//   const read = fs.createReadStream("normalize.css");
//   // 文件写入流
//   const write = fs.createWriteStream("normalize.min.css");
//   // 文件转换流
//   const transform = new Transform({
//     transform: (chunk, encoding, callback) => {
//       // 核心转换过程实现
//       // chunk => 读取流中读取到的文件内容(Buffer)
//       const input = chunk.toString();
//       // /* */ => 转义 \/\*  \*\/
//       const output = input.replace(/\s+/g, "").replace(/\/\*.+?\*\//g, "");
//       // callback 错误优先
//       callback(null, output);
//     },
//   });
//   read
//     .pipe(transform) //转换流
//     .pipe(write); //写入流
//   return read;
// };
// 文件操作 文档流 API
exports.default = () => {
  return src("src/*.css")
    .pipe(cleanCss()) //
    .pipe(rename({ extname: ".min.css" }))
    .pipe(dest("dist"));
};
